/*
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-24 11:38:57
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-24 16:33:52
 * @FilePath: /webServer/src/controller/notice/notice.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Body,
  Controller,
  Del,
  Get,
  Inject,
  Post,
  Put,
  Query,
} from '@midwayjs/core';
import { NoticeService } from '../../service/notice/notice.service';
import { Validate } from '@midwayjs/validate';
import {
  CounterValidator,
  ListParamsValidator,
  StrIdValidator,
} from '../../validator';
import {
  EditNoticeValidator,
  SaveNoticeValidator,
} from '../../validator/notice/notice';
import { NoticeSchema } from '../../schema';

@Controller('/notice', { tagName: '' })
export class NoticeController {
  @Inject()
  noticeService: NoticeService;

  @Post('/list', { summary: '列表' })
  @Validate()
  async list(@Body() body: ListParamsValidator<NoticeSchema>) {
    const data = await this.noticeService.list(body);
    return Object.assign(data, body);
  }

  @Get('/lasted_list', { summary: '获取最近添加条数' })
  @Validate()
  async lastedList(@Query() query: CounterValidator) {
    return await this.noticeService.lastedList(query.counter, { isOff: false });
  }

  @Post('/save', { summary: '保存' })
  @Validate()
  async save(@Body() body: SaveNoticeValidator) {
    return await this.noticeService.save(body);
  }

  @Get('/info', { summary: '信息' })
  @Validate()
  async info(@Query() body: StrIdValidator) {
    return await this.noticeService.info(body.id);
  }

  @Del('/remove', { summary: '删除' })
  @Validate()
  async remove(@Query() body: StrIdValidator) {
    return await this.noticeService.remove(body.id);
  }

  @Del('/delete', { summary: '（真）删除' })
  @Validate()
  async realRemove(@Query() body: StrIdValidator) {
    return await this.noticeService.realRemove(body.id);
  }

  @Put('/edit', { summary: '编辑' })
  @Validate()
  async edit(
    @Query() query: StrIdValidator,
    @Body() body: EditNoticeValidator
  ) {
    const notice = await this.noticeService.info(query.id);
    Object.assign(notice, body);
    return await this.noticeService.save(notice);
  }

  @Put('/isOff', { summary: '修改状态' })
  @Validate()
  async isOff(@Query() query: StrIdValidator) {
    const notice = await this.noticeService.info(query.id);
    Object.assign(notice, {
      isOff: !notice.isOff,
    });
    return await this.noticeService.save(notice);
  }
}
